package com.md.album.ui

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.living.filedownloader.DownLoadManger
import com.living.player.db.RecentlyManger
import com.md.album.R
import com.md.album.SelfWrapperAlbum
import com.md.album.adapter.SelfCreateAlbumAdapter
import com.md.album.databinding.ActivitySelectSelAudioBinding
import com.md.album.db.SelfAlbumManager
import com.md.livingwidget.navigation.PHNavigationBar
import com.md.network.api.Audio

/**
 * @author liyue
 * created 2021/4/5
 * desc 为新建专辑选择音频
 */
class SelectSelfAlbumActivity : AppCompatActivity() , SelfCreateAlbumAdapter.OnItemClickListener {

    private lateinit var binding: ActivitySelectSelAudioBinding
    private lateinit var recMyAlbum: RecyclerView
    private lateinit var navBar: PHNavigationBar
    private lateinit var tvTitle: TextView
    private lateinit var tvRecenterNum: TextView
    private lateinit var tvLocalNum: TextView
    private lateinit var ctlRecentPlayer: ConstraintLayout
    private lateinit var ctlDownload: ConstraintLayout
    private lateinit var searchEdit:EditText
    private var recentNum = 0
    private var localNum = 0
    private var albumId = "-1"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySelectSelAudioBinding.inflate(LayoutInflater.from(this))
        setContentView(binding.root)
        initData()
        initView()
        initNav()
    }

    private fun initView() {
        recMyAlbum = binding.recMyAlbum
        var data = getSelfAlbumData()
        var filterData = data.filterNot {
            it.album.id == albumId
        }
        var mLayoutManager = LinearLayoutManager(this)
        var mAdapter = SelfCreateAlbumAdapter(this,this, filterData)
        recMyAlbum.apply {
            layoutManager = mLayoutManager
            adapter = mAdapter
        }
        tvRecenterNum = binding.tvRecenterNum
        tvRecenterNum.text = recentNum.toString() + "首"
        tvLocalNum = binding.tvLocalNum
        tvLocalNum.text = localNum.toString() + "首"
        ctlRecentPlayer = binding.ctlRecentPlayer
        ctlRecentPlayer.setOnClickListener {
            var intent = Intent(this, SelectAudioActivity::class.java)
            intent.putExtra(DATA_TYPE, RECENT_PLAYER)
            intent.putExtra(CURRENT_ALBUM_ID, albumId)
            startActivity(intent)
        }
        searchEdit = binding.searchEdit
        searchEdit.setOnClickListener {
            ARouter.getInstance()
                    .build("/main/search")
                    .withString("SearchActivity_key_alblumId", albumId)
                    .navigation()
        }
        ctlDownload = binding.ctlDownload
        ctlDownload.setOnClickListener {
            var intent = Intent(this, SelectAudioActivity::class.java)
            intent.putExtra(DATA_TYPE, DOWNLOAD_AUDIO)
            intent.putExtra(CURRENT_ALBUM_ID, albumId)
            startActivity(intent)
        }

    }

    private fun getSelfAlbumData(): List<SelfWrapperAlbum> {
        var albumList = SelfAlbumManager.getIns().allSelfAlbum
        var res = ArrayList<SelfWrapperAlbum>()
        if (albumList.size > 0) {
            for (item in albumList) {
                var audioList = SelfAlbumManager.getIns().getAudiosByAlbumId(item.id)
                audioList?.let {
                    res.add(SelfWrapperAlbum(item, audioList.size))
                }
            }
        }
        return res

    }

    private fun initNav() {
        navBar = binding.navBar
        tvTitle = navBar.centerTvTitle
        tvTitle.text = resources.getString(R.string.album_add_album)
        navBar.right1Tv.apply {
            visibility = View.VISIBLE
            setTextColor(resources.getColor(R.color.album_595959))
            text = resources.getString(R.string.album_close)
            setOnClickListener {
                finish()
            }
        }
        navBar.left1Imgback.visibility = View.GONE
    }

    private fun initData() {
        var intent = intent
        albumId = intent.getStringExtra(CURRENT_ALBUM_ID) ?: "-1"
        var audioList = RecentlyManger.getInstance().allRecentAudios
        recentNum = audioList.size
        var localList = DownLoadManger.getInstance().downloadedList
        localNum = localList.size
    }

    override fun onItemClick(albumType: Int, curAlbumName: String, audioList: ArrayList<Audio>) {
        var intent = Intent(this, SelectAudioActivity::class.java)
            intent.putExtra(DATA_TYPE,albumType)
            intent.putExtra(CURRENT_ALBUM_ID,albumId)
            intent.putExtra(CURRENT_ALBUM_NAME,curAlbumName)
            intent.putParcelableArrayListExtra(SELF_ALBUM_AUDIO,audioList)
            startActivity(intent)
    }
}